﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;

namespace MyCommerce.Business.GeneralParameters
{
    public class CurrencyExchangeRatesManager
    {
        private MyCommerceEntities vContext;

        public CurrencyExchangeRatesManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public CurrencyExchangeRatesManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        /// <summary>
        /// Guarda un tipo de cambio.
        /// </summary>
        /// <param name="objClassifier"></param>
        public void SaveExchangeRate(CurrencyExchangeRates objClassifier)
        {
            var lastItem = (from cl in vContext.CurrencyExchangeRates
                            where cl.ValidUntil.HasValue == false
                            select cl).FirstOrDefault();
            if(lastItem!=null)
                lastItem.ValidUntil = System.DateTime.Today;
            objClassifier.ValidSince = System.DateTime.Today;
            vContext.AddToCurrencyExchangeRates(objClassifier);
            vContext.SaveChanges();
        }

        /// <summary>
        /// Elimina un tipo de cambio.
        /// </summary>
        /// <param name="idClassifier"></param>
        public void DeleteExchangeRate(long idExchangeRate)
        {
            var vResult = (from cl in vContext.CurrencyExchangeRates
                           where cl.IdCurrencyExchangeRates == idExchangeRate
                           select cl).First();            
            vContext.DeleteObject(vResult);
            vContext.SaveChanges();
        }

        public List<CurrencyExchangeRates> GetExchangeRatesList()
        {
            var vResult = (from cl in vContext.CurrencyExchangeRates
                           .Include("Classifier")
                           orderby cl.ValidSince descending
                           select cl);

            if (vResult != null)
                return new List<CurrencyExchangeRates>(vResult.AsEnumerable());

            return new List<CurrencyExchangeRates>();
        }

        public CurrencyExchangeRates CurrencyExchangeRatesGetByIdMoneyAndDate(long vIdMoney, DateTime vDate)
        {
            CurrencyExchangeRates vCurrencyExchangeRates = null;

            long moneyBase = Convert.ToInt64(MoneyEnum.LocalMoney);

            if (moneyBase == vIdMoney)
            {
                vCurrencyExchangeRates = new CurrencyExchangeRates();
                vCurrencyExchangeRates.IdcMoney = vIdMoney;
                vCurrencyExchangeRates.BuyExchangeRate = 1;
                vCurrencyExchangeRates.SellExchangeRate = 1;
                vCurrencyExchangeRates.ValidSince = System.DateTime.Today;
                vCurrencyExchangeRates.ValidUntil = null;
                return vCurrencyExchangeRates;
            }
            else
            {
                vCurrencyExchangeRates = GetExchangeRateByMoneyAndDate(vIdMoney, vDate);                
            }
            return vCurrencyExchangeRates;
        }

        private CurrencyExchangeRates GetExchangeRateByMoneyAndDate(long vIdMoney, DateTime vDate)
        {
            var result = (from ce in vContext.CurrencyExchangeRates
                          where ce.IdcMoney == vIdMoney && ce.ValidSince <= vDate
                          && (!ce.ValidUntil.HasValue || ce.ValidUntil.Value > vDate)
                          orderby ce.ValidSince descending
                          select ce).FirstOrDefault();
            return result;
        }
    }
}
